<template>
	<view>
		<view class="loginTop posRel">
			<image class="samePar posAbso" src="../../static/images/logBg_top.png"></image>
			<image src="../../static/images/logo.png" class="logo posAbso"></image>
		</view>
		<view class="loginForm">
			<button class="loginBtn" @tap="wxAuthLogin">
				<text class="iconfont icon-weixin1"></text>
				<text>{{loginTxt}}</text>
			</button>
			<view class="agree hasFlex" @tap="shouldAgree">
				<view class="check" :class="[isAgreeFlag ? 'active' : '']">
					<view class="iconfont icon-duihao"></view>
				</view>
				<view class="agreeTxt">登录即同意<text class="detTxt" @tap.stop="goFwxy">《用户协议》</text>、<text class="detTxt" @tap.stop="goYszc">《隐私政策》</text>和<text class="detTxt" @tap.stop="goSqgf">《社区规范》</text></view> 
			</view> 
		</view>
		<view class="loginBot">
			<image class="samePar" src="../../static/images/logBg_bot.png"></image>
		</view>
		<uni-popup type="bottom" ref="loginTips">
			<view class="popuoView">
				<view class="popTit">温馨提示</view>
				<view class="popCon">请您仔细阅读并充分理解相关条款,点击同意即代表您已阅读并同意<text class="specCol" @tap="goFwxy">用户协议</text><text class="specCol" @tap="goYszc">隐私政策</text>和<text class="specCol" @tap="goSqgf">社区规范</text></view>
				<view class="popBtn">
					<button class="comBtn" :disabled="isDisabled" @tap="goLoginPop">{{authBtnTit}}</button>
					<view class="noAgreeTxt" @tap="handleClose">不同意</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { wxAuthLog,checkUnionId,wxUserLogin } from '@/api/authLogin'
	export default {
		data() {
			return {
				phoneNum:'',
				password:'',
				isAgreeFlag:false,
				loadOpt : '',
				isDisabled:false,
				loginTxt:'微信授权登录',
				res:null,
				canRefreshFlag:0,//loadOpt == '' 时 0上个页面不刷新接口 1上个页面刷新接口
				authBtnTit:'同意并登录'
			}
		},
		onLoad(options){
			//userFix->关注 去预约
			this.canRefreshFlag = options.canRefreshFlag == undefined ? 0 : options.canRefreshFlag;
			this.loadOpt = options.loadOpt == undefined ? '' : options.loadOpt;
		},
		onShow(){
			if(uni.getStorageSync('logAcc') != ''){
				this.phoneNum = uni.getStorageSync('logAcc');
				this.isAgreeFlag = true;
			}
		},
		methods: {
			wxAuthLogin(){
				if(!this.isAgreeFlag){
					this.$refs.loginTips.open();
					return;
				}
				this.getUserProfile()
			},
			handleClose(){
				this.$refs.loginTips.close();
			},
			goLoginPop(){
				this.getUserProfile()
			},
			getUserProfile(){
				uni.showLoading({ title: '授权中...', mask: true });
				this.isDisabled = true;
				this.authBtnTit = '授权中...';
				uni.login({
					provider : 'weixin',
					success:(resLogin)=>{
						if(resLogin.code){
							uni.getUserInfo({
								success:(resUserInfo_1)=>{
									var field = {code:resLogin.code,appletName:'zx'};
									wxAuthLog(field).then(resAuth=>{
										uni.setStorageSync('userOpenId',resAuth.data.datas[0].openId);
										var tmpSessionKey = resAuth.data.datas[0].sessionKey,
											tmpUnionId = resAuth.data.datas[0].unionId,
											tmpOpenId = resAuth.data.datas[0].openId;
										var loginField = { encryptedData:resUserInfo_1.encryptedData,iv:resUserInfo_1.iv,sessionKey:tmpSessionKey,unionId:tmpUnionId,openId:tmpOpenId };
										var fieldCheck = {unionId:tmpUnionId};
										checkUnionId(fieldCheck).then(res=>{//直接登录
											uni.showLoading({ title: '登录中...', mask: true });
											this.authBtnTit = '登录中...';
											var tmpLogField = { mobile:res.data.datas,opt:'visitor',client:'applet' }
											Object.assign( loginField,tmpLogField );
											//console.log(loginField)
											wxUserLogin(loginField).then(res=>{
												//console.log(res)
												this.User.token = res.data.datas.token;
												this.User.userIdPss = res.data.datas.userId;
												uni.setStorageSync("token", this.User.token);
												uni.setStorageSync("userIdPss",this.User.userIdPss);
												uni.setStorageSync('logAcc',res.data.datas.account);
												uni.setStorageSync('userPortrait',res.data.datas.userPortrait);
												uni.setStorageSync('nickName',res.data.datas.netName);
												uni.showToast({ title: '登录成功' });
												/* 执行即时通讯登录 */
												//this.User.__loadIM(res.data.datas.account);
												uni.$emit('changeLoginStaToLogin');
												if(this.loadOpt == ''){
													setTimeout(()=>{
														uni.switchTab({
															url:'/pages/index/index'
														})
													},1500);
												}else if(this.loadOpt == 'userFixNoRef'){
													uni.navigateBack({
														delta:1
													})
												}else{
													if(this.canRefreshFlag == 1){
														let pages = getCurrentPages();
														let prevPage = pages[pages.length - 2];
														prevPage.$vm.isRefreshFlag = true;
													}
													uni.navigateBack({
														delta:1
													})
												}
											}).catch(err=>{})
										}).catch(err=>{
											if(err.data.code == 50001){
												uni.setStorageSync('loginField',loginField);
												uni.navigateTo({
													url:'../bindPhone/bindPhone?loadOpt=' + this.loadOpt + '&canRefreshFlag=' + this.canRefreshFlag,
													success:()=>{
														this.$refs.loginTips.close();
														this.isDisabled = false;
														this.authBtnTit = '同意并登录';
													}
												})
											}
										})
									}).catch(err=>{
										console.log(err)
									})
								},fail:(err)=>{
									// console.log(err)
									uni.hideLoading();
									this.isDisabled = false;
									this.authBtnTit = '同意并登录';
									this.$refs.loginTips.close();
									this.util.showToast('为了您更好的体验,请先同意授权');
								}
							})
						}
					},
					fail:(err)=>{
						console.log('loginFail=' + err)
					}
				})
			},
			shouldAgree(){
				this.isAgreeFlag = !this.isAgreeFlag;
			},
			goFwxy(){
				uni.navigateTo({
					url:'../protocol/protocol?xyId=1'
				})
			},
			goYszc(){
				uni.navigateTo({
					url:'../protocol/protocol?xyId=2'
				})
			},
			//社区规范
			goSqgf(){
				uni.navigateTo({
					url:'../protocol/protocol?xyId=4'
				})
			},
		}
	}
</script>

<style lang="scss">
	@import '@/static/scss/login.scss'; 
	page{background:#fff;}
</style>
